package pub.gll.onepeas.module.cookbook.vm

import androidx.lifecycle.ViewModel
import androidx.lifecycle.asFlow
import androidx.lifecycle.asLiveData
import androidx.lifecycle.viewModelScope
import androidx.paging.cachedIn
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import pub.gll.onepeas.module.cookbook.repository.CookbookRepository
import javax.inject.Inject

@HiltViewModel
class CookbookVM @Inject constructor(
    comicHomeRepository: CookbookRepository
): ViewModel() {
    fun s(){
        viewModelScope.launch(Dispatchers.IO) {
            repeat(10){

            }
            //可以拿到返回结果
            val job =  async(Dispatchers.IO) {
                "1111"
            }
            //拿到返回结果
            println("job=${job.await()}")
            //用于协程中切换线程
            withContext(Dispatchers.IO){

            }

        }
    }

    var data = comicHomeRepository.fetchCookbookList()
        .cachedIn(viewModelScope)
        .asLiveData()
        .asFlow()

}